import { defineStore } from 'pinia'

import type { CategoryStateModel } from '@/api/product/model/CategoryStateModel'

//引入接口函数
import { reqGetCategory1ListData, reqGetCategory2ListData, reqGetCategory3ListData } from '@/api/product/category'

//创建模块对象
export const useCategoryListStore = defineStore('categoryList', {
    state: (): CategoryStateModel => {
        return {
            //存储用户选择的分类id
            category1Id: undefined,
            category2Id: undefined,
            category3Id: undefined,

            //存储三级分类数据
            category1List: [],
            category2List: [],
            category3List: [],
        }
    },
    actions: {
        //获取一级分类数据
        async getCategory1ListData() {
            const result = await reqGetCategory1ListData()
            this.category1List = result
        },
        //获取二级分类数据
        async getCategory2ListData(category1Id: number) {
            const result = await reqGetCategory2ListData(category1Id)
            this.category2List = result
            this.category1Id = category1Id
            //一级导航id变化，二级三级数据要重置
            this.category2Id = undefined
            this.category3Id = undefined
            this.category3List = []
        },
        //获取三级分类数据
        async getCategory3ListData(category2Id: number) {
            const result = await reqGetCategory3ListData(category2Id)
            this.category3List = result
            this.category2Id = category2Id
            //二级导航变化，三级数据要重置
            this.category3Id = undefined
        },
        //设置三级分类Id值
        async setCategory3Id(category3Id: number) {
            this.category3Id = category3Id
        }
    },
})